package com.yl.auth.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yl.auth.entity.Jigo;
import com.yl.auth.service.JigoService;
import com.yl.auth.service.JingyingxinxiService;
import com.yl.common.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.UUID;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ln
 * @since 2023-07-20
 */
@Api(tags = "运营端-机构管理")
@RestController
@RequestMapping("/admin/auth/jigo")
public class JigoController {
    @Resource
    private JigoService jigoService;
    @ApiOperation(value = "机构全查")
    @GetMapping("/findjigo/{page}/{limit}")
    public Result findjigo(@PathVariable Integer page, @PathVariable Integer limit, Jigo jigo){
        Page<Jigo> page1 = new Page<>(page, limit);
        IPage<Jigo> jigoIPage = jigoService.findjigo(page1,jigo);
        return Result.ok(jigoIPage);
    }
    @ApiOperation(value = "删除机构")
    @DeleteMapping("/remove/{id}")
    public Result remove(@PathVariable Integer id){
        jigoService.removeById(id);
        return Result.ok();
    }
    @ApiOperation(value = "添加机构")
    @PostMapping("/save")
    public Result save(@RequestBody Jigo jigo){
        jigoService.save(jigo);
        return Result.ok();
    }
    /*图片上传上传图片接口*/
    /*图片上传地址*/
    String fileSavePath = "D:/vscode/codeylhomes/public/upload/";
    /*格式化时间*/
    private SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd/");
    @PostMapping("/saveUpload")
    @ApiOperation(value = "图片上传yingyezhizhao字段")
    @CrossOrigin
    public Result uploadFile (@ApiParam(name = "file", value = "上传", required = true) @RequestPart("file") MultipartFile file, HttpServletRequest request){
        /*将时间转化成字符串*/
        Date date = new Date();
        String directory = simpleDateFormat.format(date);
        /*文件路径是否存在
         * 如果不存在,则创建
         */
        File dir = new File(fileSavePath + directory);
        if (!dir.exists()) {
            dir.mkdirs();
        }
        /*获取图片的名字*/
        String imageName = file.getOriginalFilename();
        System.out.println("图片上传位置" + fileSavePath + directory);
        /*给文件创建一个新的名字*/
        String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
        /*图片新的名字*/
        String newFileName = UUID.randomUUID().toString().replaceAll("-", "") + suffix;
        System.out.println("上传的图片位置" + fileSavePath + directory + newFileName);
        /*创建新文件*/
        File newFile = new File(fileSavePath + directory + newFileName);
        try {
            file.transferTo(newFile);
            String url = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + "/images/" + directory + newFileName;
            System.out.println("图片上传成功的url:" + url);
            String sql = "/upload/" + directory + newFileName;
            /*返回给前端的字符串地址,然后添加的时候将这一串字符串加入到数据库中*/
            System.out.println(sql);
            return Result.ok(sql);
        } catch (IOException e) {
            return Result.ok();
        }
    }

    @PostMapping("/saveUpload2")
    @ApiOperation(value = "图片上传机构图片字段")
    @CrossOrigin
    public Result uploadFile2 (@ApiParam(name = "file", value = "上传", required = true) @RequestPart("file") MultipartFile file, HttpServletRequest request){
        /*将时间转化成字符串*/
        Date date = new Date();
        String directory = simpleDateFormat.format(date);
        /*文件路径是否存在
         * 如果不存在,则创建
         */
        File dir = new File(fileSavePath + directory);
        if (!dir.exists()) {
            dir.mkdirs();
        }
        /*获取图片的名字*/
        String imageName = file.getOriginalFilename();
        System.out.println("图片上传位置" + fileSavePath + directory);
        /*给文件创建一个新的名字*/
        String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
        /*图片新的名字*/
        String newFileName = UUID.randomUUID().toString().replaceAll("-", "") + suffix;
        System.out.println("上传的图片位置" + fileSavePath + directory + newFileName);
        /*创建新文件*/
        File newFile = new File(fileSavePath + directory + newFileName);
        try {
            file.transferTo(newFile);
            String url = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + "/images/" + directory + newFileName;
            System.out.println("图片上传成功的url:" + url);
            String sql = "/upload/" + directory + newFileName;
            /*返回给前端的字符串地址,然后添加的时候将这一串字符串加入到数据库中*/
            return Result.ok(sql);
        } catch (IOException e) {
            return Result.ok();
        }
    }
    @ApiOperation(value = "根据id查询机构")
    @GetMapping("/getById/{id}")
    public Result getById(@PathVariable Integer id){
        Jigo byId = jigoService.getById(id);
        return Result.ok(byId);
    }
    @ApiOperation("根据id修改机构信息")
    @PostMapping("/update")
    public Result update(@RequestBody Jigo jigo){
        jigoService.updateById(jigo);
        return Result.ok();
    }
}
